package com.xuxin.nantai.model;

import com.xuxin.nantai.base.BaseConstant;
import com.xuxin.nantai.base.BaseObserver;
import com.xuxin.nantai.bean.BaseBean;
import com.xuxin.nantai.bean.HangUpdateLoginBean;
import com.xuxin.nantai.bean.PackInfoBean;
import com.xuxin.nantai.contract.PackUpdateContract;
import com.xuxin.nantai.network.RetrofitApi;
import com.xuxin.nantai.network.Retrofits;
import com.xuxin.nantai.presenter.PackUpdatePresenter;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;


/**
 * Created by xu_xin
 * Created on 2020.06.22 0022
 */
public class PackUpdateModel implements PackUpdateContract.Model {
    @Override
    public void getPackInfo(PackUpdatePresenter presenter, String FlowingCode, String LoginName) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .getPackInfo(FlowingCode, LoginName)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<PackInfoBean>() {
            @Override
            public void onNext(PackInfoBean bean) {
                if (BaseConstant.success_code.equals(bean.getCode())) {
                    presenter.onSuccess(bean);
                } else {
                    presenter.onError(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                presenter.onError(null);
            }

            @Override
            public void onComplete() {
                presenter.onComplete();
            }
        });
    }

    @Override
    public void update(PackUpdatePresenter presenter,
                       String LoginName,
                       String UserName,
                       String Guid,
                       String ID,
                       String cInvName,
                       String cInvCode,
                       String Color,
                       String PackQty,
                       String Check_BZ,
                       String CheckNum,
                       String FlowingCode,
                       String Remark,
                       String Result,
                       String InspectorName,
                       String InspectorCode,
                       String InspectTime,
                       String FacArea,
                       String DefectNum,
                       String DefectDes,
                       int IsReCheck) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .updatePack(LoginName,
                        UserName,
                        Guid,
                        ID,
                        cInvName,
                        cInvCode,
                        Color,
                        PackQty,
                        Check_BZ,
                        CheckNum,
                        FlowingCode,
                        Remark,
                        Result,
                        InspectorName,
                        InspectorCode,
                        InspectTime,
                        FacArea,
                        DefectNum,
                        DefectDes,
                        IsReCheck)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseBean>() {
            @Override
            public void onNext(BaseBean bean) {
                if (BaseConstant.success_code.equals(bean.getCode())) {
                    presenter.onSuccess(bean);
                } else {
                    presenter.onError(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                presenter.onError(null);
            }

            @Override
            public void onComplete() {
                presenter.onComplete();
            }
        });
    }

    @Override
    public void getLoginUser(PackUpdatePresenter presenter, String loginName) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .getLoginUser(loginName)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<HangUpdateLoginBean>() {
            @Override
            public void onNext(HangUpdateLoginBean bean) {
                if (BaseConstant.success_code.equals(bean.getCode())) {
                    presenter.onSuccess(bean);
                } else {
                    presenter.onError(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                presenter.onError(null);
            }

            @Override
            public void onComplete() {
                presenter.onComplete();
            }
        });
    }
}